package cn.wuxuan.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.gzsxt.po.PageResult;
import cn.wuxuan.po.QueryObject;
import cn.wuxuan.po.Result;
import cn.wuxuan.po.Task;
import cn.wuxuan.po.TaskTrans;
import cn.wuxuan.service.UserService;
import cn.wuxuan.service.Impl.UserServiceImpl;
import cn.wuxuan.vo.Msg;

import com.alibaba.fastjson.JSON;

@WebServlet("/task.oas")
public class TaskServlet extends HttpServlet {
	
	UserService service = new UserServiceImpl();
	private static final long serialVersionUID = 1L;
	

	
	
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		System.out.println("这边是正确的");
		req.setCharacterEncoding("utf-8");
		resp.setContentType("appliction/json;charset=utf-8");
		PrintWriter out = resp.getWriter();
			String cmd = req.getParameter("cmd");
			if("checkTask".equals(cmd)){
				String title = req.getParameter("title");
				Integer count = service.checkTitle(title);
				if(count==1){
					out.print(false);
				}else {
					out.print(true);
				}
			}
			
			if("addTask".equals(cmd)){
				String title = req.getParameter("title");
				String content = req.getParameter("content");
				String annex = req.getParameter("annex");
				Integer status =Integer.valueOf( req.getParameter("status"));
				Integer urgent =Integer.valueOf( req.getParameter("urgent"));
				String person = req.getParameter("person");
				Integer dept =Integer.valueOf( req.getParameter("dept"));
				String datastring1 = req.getParameter("createtime");
				System.out.println(datastring1);
				String  datastring2 = req.getParameter("strtime");
				String  datastring3 = req.getParameter("fintime");
				SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
				Date createtime  = null;
				Date strtime = null;
				Date fintime = null;
				try {
					createtime =  format.parse(datastring1);
					strtime =  format.parse(datastring2);
					fintime =  format.parse(datastring3);
				} catch (ParseException e) {
					e.printStackTrace();
				}
				Task task = new Task(title, content, annex, status, urgent, person, dept, createtime, strtime, fintime);
				Integer count = service.addTask(task);
				if(count>=2){
					out.print(JSON.toJSONString(new Msg(1,"插入成功")));
				}else{
					out.print(JSON.toJSONString(new Msg(0,"插入失败")));
				}
			}
			
			if("selectTask".equals(cmd)){
				String currentPage = req.getParameter("currentPage");
				String pageSize =  req.getParameter("pageSize");
				String keyWord = req.getParameter("keyWord");
			QueryObject qo = new QueryObject();
			if(currentPage!=null){
				qo.setCurrentPage(Integer.valueOf(currentPage));
			}
			if(pageSize!=null){
				qo.setPageSize(Integer.valueOf(pageSize));
			}
			if(keyWord!=null&&!"".equals(keyWord.trim())){
				qo.setKeyWord(keyWord);
			}
			
			PageResult result  = service.selectTask(qo);
			req.setAttribute("pageResult", result);
			req.setAttribute("keyWord", keyWord);
		
			req.getRequestDispatcher("wuxuan/taskApprove.jsp?time="+new Date().getTime()).forward(req, resp);
			}
			
			if("deleteTask".equals(cmd)){
				String title = req.getParameter("title");
				if(title!=null&&!"".equals(title)){
					Integer count = service.deleteTask(title);
					if(count>=2){
						out.print(JSON.toJSONString(new Msg(1,"删除成功")));
					}else{
						out.print(JSON.toJSONString(new Msg(0,"删除失败")));
					}
				}else {
					out.print(JSON.toJSONString(new Msg(0,"删除失败")));
				}
			}
			
			
			if("changeTask".equals(cmd)){
				String title = req.getParameter("chtitle");
				String clerk = req.getParameter("chclerk");
				String reason = req.getParameter("chreason");
				Integer status = Integer.valueOf(req.getParameter("chstatus"));
				String acceptor = req.getParameter("chacceptor");
				String opinion = req.getParameter("chopinion");
				
				TaskTrans trans = new TaskTrans();
				trans.setTitle(title);
				trans.setTransreason(reason);
				trans.setDelayclerk(clerk);
				trans.setAcceptor(acceptor);
				trans.setTransopinion(opinion);
				
				Integer count = service.changeTask(trans);
					if(count>=1){
						out.print(JSON.toJSONString(new Msg(1,"修改成功")));
					}else{
						out.print(JSON.toJSONString(new Msg(0,"修改失败")));
					}
			}
			
			
			
			if("selectResult".equals(cmd)){
				System.out.println("成功访问了");
				String currentPage = req.getParameter("currentPage");
				String pageSize =  req.getParameter("pageSize");
				String keyWord = req.getParameter("keyWord");
			QueryObject qo = new QueryObject();
			if(currentPage!=null){
				qo.setCurrentPage(Integer.valueOf(currentPage));
			}
			if(pageSize!=null){
				qo.setPageSize(Integer.valueOf(pageSize));
			}
			if(keyWord!=null&&!"".equals(keyWord.trim())){
				qo.setKeyWord(keyWord);
			}
			
			PageResult result = service.selectResult(qo);
			req.setAttribute("Result", result);
			System.out.println(result.getDataList().toString());
			req.setAttribute("keyWord", keyWord);
			
			req.getRequestDispatcher("wuxuan/resultSubmit.jsp?time="+new Date().getTime()).forward(req, resp);
			}
			
			
			if("changeResult".equals(cmd)){
				String title = req.getParameter("resulttitle");
				String resultDir = req.getParameter("resultdir");
				String resultFile= req.getParameter("resultfile");
				Result result = new Result();
				result.setTitle(title);
				result.setAchieve(resultDir);
				result.setAchievefile(resultFile);
				Integer count = service.changeResult(result);
				if(count>=1){
					out.print(JSON.toJSONString(new Msg(1,"修改成功")));
				}else{
					out.print(JSON.toJSONString(new Msg(0,"修改失败")));
				}
			}
	}

}
